package com.example.demo;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redis")
public class RedisController {

	ValueOperations<String, String> redisOperations;
	
	RedisController(StringRedisTemplate template){
		redisOperations = template.opsForValue();
	}
	/**
	 * http://localhost:8080/redis/set?key=wkq&value=boy
	 * @param key
	 * @param value
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/set")
	public void set(final String key,String value,HttpServletResponse response) throws IOException {
		redisOperations.set(key, value);
		response.sendRedirect("get?key="+key);
	}

	@RequestMapping("/get")
	public String get(final String key) {
		String str = redisOperations.get(key);
		return str;
	}

	@Autowired
	private RedisTemplate redisTemplate;
	@RequestMapping("/getUser")
	public Object getUser() {
		Object user = redisTemplate.opsForValue().get("hnu");
		return user;
	}
}
